package com.bilibili.juc.arthas;

import java.util.Arrays;

/**
 * 冒泡排序 进行了优化
 */
public class PaoSortHigh {
    public static void main(String[] args) {
//        int[] arr = {5,3,2,1};
        int[] arr = {5,2,1,3};
//        int[] arr = {2,1,3,5,6,8,9,10,11,23,30,35};
//        int[] arr = {30,11,23,10,35,2,1,3,5,6,8,9};
        int length = arr.length;
        //判断后面是否又排序了，如果没有直接跳出循环，提升性能
        boolean isSort = false;
        for (int i = 0; i < length-1; i++) {
            int ilen = length-i-1;
            System.out.println("i==>> " + i);
            for (int j = 0; j < ilen; j++) {
                if (arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    isSort = true;
                }
            }
            if (!isSort){
                break;
            }
            isSort = false;
        }

        System.out.println(Arrays.toString(arr));
    }
}
